home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow_WinXP / VMR / VMRPlayer / commands.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  12.0 KB  |  487 lines

  1. //------------------------------------------------------------------------------
  2. // File: commands.cpp
  3. //
  4. // Desc: DirectShow sample code
  5. //       - Processes commands from the user.
  6. //
  7. // Copyright (c) 1994 - 2001, Microsoft Corporation.  All rights reserved.
  8. //------------------------------------------------------------------------------
  9. #include <streams.h>
  10. #include "project.h"
  11.  
  12. typedef LPBITMAPINFOHEADER PDIB;
  13.  
  14. extern CMpegMovie *pMpegMovie;
  15. extern void RepositionMovie(HWND hwnd);
  16.  
  17. // Constants
  18. #define BFT_BITMAP 0x4d42   /* 'BM' */
  19.  
  20. // Macros
  21. #define DibNumColors(lpbi)      ((lpbi)->biClrUsed == 0 && (lpbi)->biBitCount <= 8 \
  22.                                     ? (int)(1 << (int)(lpbi)->biBitCount)          \
  23.                                     : (int)(lpbi)->biClrUsed)
  24.  
  25. #define DibSize(lpbi)           ((lpbi)->biSize + (lpbi)->biSizeImage + (int)(lpbi)->biClrUsed * sizeof(RGBQUAD))
  26.  
  27. #define DibPaletteSize(lpbi)    (DibNumColors(lpbi) * sizeof(RGBQUAD))
  28.  
  29.  
  30. /******************************Public*Routine******************************\
  31. * VcdPlyerCaptureImage
  32. *
  33. \**************************************************************************/
  34. BOOL
  35. VcdPlyerCaptureImage(
  36.     LPCSTR szFile
  37.     )
  38. {
  39.     if(pMpegMovie)
  40.     {
  41.         BYTE* lpCurrImage = NULL;
  42.  
  43.         if(pMpegMovie->GetCurrentImage(&lpCurrImage) == S_OK)
  44.         {
  45.             BITMAPFILEHEADER    hdr;
  46.             HFILE               fh;
  47.             OFSTRUCT            of;
  48.             DWORD               dwSize;
  49.             PDIB                pdib = (PDIB)lpCurrImage;
  50.  
  51.             fh = OpenFile(szFile,&of,OF_CREATE|OF_READWRITE);
  52.  
  53.             if(fh == -1)
  54.                 return FALSE;
  55.  
  56.             dwSize = DibSize(pdib);
  57.  
  58.             hdr.bfType          = BFT_BITMAP;
  59.             hdr.bfSize          = dwSize + sizeof(BITMAPFILEHEADER);
  60.             hdr.bfReserved1     = 0;
  61.             hdr.bfReserved2     = 0;
  62.             hdr.bfOffBits       = (DWORD)sizeof(BITMAPFILEHEADER) + pdib->biSize +
  63.                 DibPaletteSize(pdib);
  64.  
  65.             _lwrite(fh,(LPCSTR)(LPVOID)&hdr,sizeof(BITMAPFILEHEADER));
  66.             _hwrite(fh,(LPCSTR)(LPVOID)pdib,dwSize);
  67.  
  68.             _lclose(fh);
  69.  
  70.             CoTaskMemFree(lpCurrImage);
  71.  
  72.             TCHAR szText[128], szDir[MAX_PATH];
  73.             GetCurrentDirectory(MAX_PATH, szDir);
  74.             wsprintf(szText, TEXT("Captured current image to %s\\%s."), szDir, szFile);
  75.             MessageBox(hwndApp, szText, TEXT("Captured bitmap"), MB_OK);
  76.             return TRUE;
  77.         }
  78.     }
  79.  
  80.     return FALSE;
  81. }
  82.  
  83.  
  84. /******************************Public*Routine******************************\
  85. * VcdPlyerDisplayCapturedImage
  86. *
  87. \**************************************************************************/
  88. BOOL
  89. VcdPlyerDisplayCapturedImage(
  90.     LPCSTR szFile
  91.     )
  92. {
  93.     // Open the bitmap with the system-default application
  94.     ShellExecute(hwndApp, "open\0", szFile, NULL, NULL, SW_SHOWNORMAL);
  95.  
  96.     return TRUE;
  97. }
  98.  
  99.  
  100. /******************************Public*Routine******************************\
  101. * VcdPlayerOpenCmd
  102. *
  103. \**************************************************************************/
  104. BOOL
  105. VcdPlayerOpenCmd(
  106.     int strmID
  107.     )
  108. {
  109.     static BOOL fFirstTime = TRUE;
  110.     BOOL fRet;
  111.     TCHAR achFileName[MAX_PATH];
  112.     TCHAR achFilter[MAX_PATH];
  113.     LPTSTR lp;
  114.  
  115.     if(fFirstTime)
  116.     {
  117.         ofn.lStructSize = sizeof(ofn);
  118.         ofn.hwndOwner = hwndApp;
  119.         ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST |
  120.             OFN_SHAREAWARE | OFN_PATHMUSTEXIST;
  121.     }
  122.  
  123.     lstrcpy(achFilter, IdStr(STR_FILE_FILTER));
  124.     ofn.lpstrFilter = achFilter;
  125.  
  126.     /*
  127.     ** Convert the resource string into to something suitable for
  128.     ** GetOpenFileName ie.  replace '#' characters with '\0' characters.
  129.     */
  130.     for(lp = achFilter; *lp; lp++)
  131.     {
  132.         if(*lp == TEXT('#'))
  133.         {
  134.             *lp = TEXT('\0');
  135.         }
  136.     }
  137.  
  138.     ofn.lpstrFile = achFileName;
  139.     ofn.nMaxFile = sizeof(achFileName) / sizeof(TCHAR);
  140.     ZeroMemory(achFileName, sizeof(achFileName));
  141.  
  142.     fRet = GetOpenFileName(&ofn);
  143.     if(fRet)
  144.     {
  145.         if(strmID == 0)
  146.         {
  147.             fFirstTime = FALSE;
  148.             ProcessOpen(achFileName);
  149.         }
  150.         else
  151.         {
  152.             if(pMpegMovie)
  153.             {
  154.                 pMpegMovie->RenderSecondFile(achFileName);
  155.             }
  156.         }
  157.  
  158.         InitStreamParams(strmID);
  159.     }
  160.  
  161.     return fRet;
  162. }
  163.  
  164.  
  165. /******************************Public*Routine******************************\
  166. * VcdPlayerCloseCmd
  167. *
  168. \**************************************************************************/
  169. BOOL
  170. VcdPlayerCloseCmd(
  171.     void
  172.     )
  173. {
  174.     if(pMpegMovie)
  175.     {
  176.         LONG cx, cy;
  177.  
  178.         g_State = VCD_NO_CD;
  179.         pMpegMovie->GetMoviePosition(&lMovieOrgX, &lMovieOrgY, &cx, &cy);
  180.         pMpegMovie->StopMovie();
  181.         pMpegMovie->CloseMovie();
  182.  
  183.         SetDurationLength((REFTIME)0);
  184.         SetCurrentPosition((REFTIME)0);
  185.  
  186.         delete pMpegMovie;
  187.         pMpegMovie = NULL;
  188.     }
  189.  
  190.     g_bSecondFileLoaded = FALSE;
  191.     InvalidateRect(hwndApp, NULL, FALSE);
  192.     UpdateWindow(hwndApp);
  193.     return TRUE;
  194. }
  195.  
  196.  
  197. /******************************Public*Routine******************************\
  198. * VcdPlayerPlayCmd
  199. *
  200. \**************************************************************************/
  201. BOOL
  202. VcdPlayerPlayCmd(
  203.     void
  204.     )
  205. {
  206.     BOOL fStopped = (g_State & VCD_STOPPED);
  207.     BOOL fPaused  = (g_State & VCD_PAUSED);
  208.  
  209.     if((fStopped || fPaused))
  210.     {
  211.         if(pMpegMovie)
  212.         {
  213.             pMpegMovie->PlayMovie();
  214.         }
  215.  
  216.         g_State &= ~(fStopped ? VCD_STOPPED : VCD_PAUSED);
  217.         g_State |= VCD_PLAYING;
  218.     }
  219.  
  220.     return TRUE;
  221. }
  222.  
  223.  
  224. /******************************Public*Routine******************************\
  225. * VcdPlayerPlayCmd
  226. *
  227. \**************************************************************************/
  228. BOOL
  229. VcdPlayerStopCmd(
  230.     void
  231.     )
  232. {
  233.     BOOL fPlaying = (g_State & VCD_PLAYING);
  234.     BOOL fPaused  = (g_State & VCD_PAUSED);
  235.  
  236.     if((fPlaying || fPaused))
  237.     {
  238.         if(pMpegMovie)
  239.         {
  240.             pMpegMovie->StopMovie();
  241.             SetCurrentPosition(pMpegMovie->GetCurrentPosition());
  242.         }
  243.  
  244.         g_State &= ~(fPlaying ? VCD_PLAYING : VCD_PAUSED);
  245.         g_State |= VCD_STOPPED;
  246.     }
  247.     return TRUE;
  248. }
  249.  
  250.  
  251. /******************************Public*Routine******************************\
  252. * VcdPlayerStepCmd
  253. *
  254. \**************************************************************************/
  255. BOOL
  256. VcdPlayerStepCmd(
  257.     void
  258.     )
  259. {
  260.     if(pMpegMovie)
  261.     {
  262.         // Ensure that the video is paused to update toolbar buttons
  263.         if(g_State & VCD_PLAYING)
  264.             VcdPlayerPauseCmd();
  265.  
  266.         if(pMpegMovie->FrameStepMovie())
  267.         {
  268.             g_State |= VCD_STEPPING;
  269.             return TRUE;
  270.         }
  271.     }
  272.     return FALSE;
  273. }
  274.  
  275.  
  276. /******************************Public*Routine******************************\
  277. * VcdPlayerPauseCmd
  278. *
  279. \**************************************************************************/
  280. BOOL
  281. VcdPlayerPauseCmd(
  282.     void
  283.     )
  284. {
  285.     BOOL fPlaying = (g_State & VCD_PLAYING);
  286.     BOOL fPaused  = (g_State & VCD_PAUSED);
  287.  
  288.     if(fPlaying)
  289.     {
  290.         if(pMpegMovie)
  291.         {
  292.             pMpegMovie->PauseMovie();
  293.             SetCurrentPosition(pMpegMovie->GetCurrentPosition());
  294.         }
  295.  
  296.         g_State &= ~VCD_PLAYING;
  297.         g_State |= VCD_PAUSED;
  298.     }
  299.     else if(fPaused)
  300.     {
  301.         if(pMpegMovie)
  302.         {
  303.             pMpegMovie->PlayMovie();
  304.         }
  305.  
  306.         g_State &= ~VCD_PAUSED;
  307.         g_State |= VCD_PLAYING;
  308.     }
  309.  
  310.     return TRUE;
  311. }
  312.  
  313.  
  314. /******************************Public*Routine******************************\
  315. * VcdPlayerSeekCmd
  316. *
  317. \**************************************************************************/
  318. void
  319. VcdPlayerSeekCmd(
  320.     REFTIME rtSeekBy
  321.     )
  322. {
  323.     REFTIME rt;
  324.     REFTIME rtDur;
  325.  
  326.     rtDur = pMpegMovie->GetDuration();
  327.     rt = pMpegMovie->GetCurrentPosition() + rtSeekBy;
  328.  
  329.     rt = max(0, min(rt, rtDur));
  330.  
  331.     pMpegMovie->SeekToPosition(rt,TRUE);
  332.     SetCurrentPosition(pMpegMovie->GetCurrentPosition());
  333. }
  334.  
  335.  
  336. /******************************Public*Routine******************************\
  337. * ProcessOpen
  338. *
  339. \**************************************************************************/
  340. void
  341. ProcessOpen(
  342.     TCHAR *achFileName,
  343.     BOOL bPlay
  344.     )
  345. {
  346.     /*
  347.     ** If we currently have a video loaded we need to discard it here.
  348.     */
  349.     if(g_State & VCD_LOADED)
  350.     {
  351.         VcdPlayerCloseCmd();
  352.     }
  353.  
  354.     lstrcpy(g_achFileName, achFileName);
  355.  
  356.     pMpegMovie = new CMpegMovie(hwndApp);
  357.     if(pMpegMovie)
  358.     {
  359.         HRESULT hr = pMpegMovie->OpenMovie(g_achFileName);
  360.         if(SUCCEEDED(hr))
  361.         {
  362.             TCHAR achTmp[MAX_PATH];
  363.  
  364.             nRecentFiles = SetRecentFiles(achFileName, nRecentFiles);
  365.  
  366.             wsprintf(achTmp, IdStr(STR_APP_TITLE_LOADED),
  367.                 g_achFileName);
  368.             g_State = (VCD_LOADED | VCD_STOPPED);
  369.  
  370.             // SetDurationLength(pMpegMovie->GetDuration());
  371.             g_TimeFormat = VcdPlayerChangeTimeFormat(g_TimeFormat);
  372.  
  373.             RepositionMovie(hwndApp);
  374.             pMpegMovie->SetBorderClr(RGB(0x00, 0x80, 0x80));
  375.  
  376.             //  If play
  377.             if(bPlay)
  378.             {
  379.                 pMpegMovie->PlayMovie();
  380.             }
  381.         }
  382.         else
  383.         {
  384.             TCHAR Buffer[MAX_ERROR_TEXT_LEN];
  385.  
  386.             if(AMGetErrorText(hr, Buffer, MAX_ERROR_TEXT_LEN))
  387.             {
  388.                 MessageBox(hwndApp, Buffer,
  389.                     IdStr(STR_APP_TITLE), MB_OK);
  390.             }
  391.             else
  392.             {
  393.                 MessageBox(hwndApp,
  394.                     TEXT("Failed to open the movie.  Either the file was ")
  395.                     TEXT("not found or the wave device is in use."),
  396.                     IdStr(STR_APP_TITLE), MB_OK);
  397.             }
  398.  
  399.             pMpegMovie->CloseMovie();
  400.             delete pMpegMovie;
  401.             pMpegMovie = NULL;
  402.         }
  403.     }
  404.  
  405.     InitStreamParams(0);
  406.  
  407.     InvalidateRect(hwndApp, NULL, FALSE);
  408.     UpdateWindow(hwndApp);
  409. }
  410.  
  411.  
  412. /******************************Public*Routine******************************\
  413. * VcdPlayerChangeTimeFormat
  414. *
  415. * Tries to change the time format to id.  Returns the time format that
  416. * actually got set.  This may differ from id if the graph does not support
  417. * the requested time format.
  418. *
  419. \**************************************************************************/
  420. int
  421. VcdPlayerChangeTimeFormat(
  422.     int id
  423.     )
  424. {
  425.     // Menu items are disabled while we are playing
  426.     BOOL    bRet = FALSE;
  427.     int     idActual = id;
  428.  
  429.     ASSERT(pMpegMovie);
  430.     ASSERT(pMpegMovie->StatusMovie() != MOVIE_NOTOPENED);
  431.  
  432.     // Change the time format with the filtergraph
  433.     switch(id)
  434.     {
  435.         case IDM_FRAME:
  436.             bRet = pMpegMovie->SetTimeFormat(TIME_FORMAT_FRAME);
  437.             break;
  438.  
  439.         case IDM_FIELD:
  440.             bRet = pMpegMovie->SetTimeFormat(TIME_FORMAT_FIELD);
  441.             break;
  442.  
  443.         case IDM_SAMPLE:
  444.             bRet = pMpegMovie->SetTimeFormat(TIME_FORMAT_SAMPLE);
  445.             break;
  446.  
  447.         case IDM_BYTES:
  448.             bRet = pMpegMovie->SetTimeFormat(TIME_FORMAT_BYTE);
  449.             break;
  450.     }
  451.  
  452.     if(!bRet)
  453.     {
  454.         // IDM_TIME and all other cases,  everyone should support IDM_TIME
  455.         bRet = pMpegMovie->SetTimeFormat(TIME_FORMAT_MEDIA_TIME);
  456.         ASSERT(bRet);
  457.         idActual = IDM_TIME;
  458.     }
  459.  
  460.     // Pause the movie to get a current position
  461.  
  462.     SetDurationLength(pMpegMovie->GetDuration());
  463.     SetCurrentPosition(pMpegMovie->GetCurrentPosition());
  464.  
  465.     return idActual;
  466. }
  467.  
  468.  
  469. /******************************Public*Routine******************************\
  470. * VcdPlayerRewindCmd
  471. *
  472. \**************************************************************************/
  473. BOOL
  474. VcdPlayerRewindCmd(
  475.     void
  476.     )
  477. {
  478.     if(pMpegMovie)
  479.     {
  480.         pMpegMovie->SeekToPosition((REFTIME)0,FALSE);
  481.     }
  482.  
  483.     return TRUE;
  484. }
  485.  
  486.  
  487.